<template>
  <basic-container>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-setting"></i> 销售订单管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <i class="el-icon-date"></i> 审核
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <avue-form ref="form" v-model="form" :option="reviewOption">
      <template slot-scope="scope" slot="menuForm">
        <el-button type="success" @click="handleSuccess" :loading="btnLoading">通 过</el-button>
        <el-button type="warning" @click="handleFailed" :loading="btnLoading">不通过</el-button>
        <el-button @click="goBack">返 回</el-button>
      </template>
    </avue-form>
    <avue-detail ref="detail_form" v-model="showForm" :option="option"></avue-detail>
    <avue-crud ref="crud" :option="orderDetailOption" :data="orderDetailList">
      <template slot="goodsImg" slot-scope="scope">
        <img width="60px" height="60px" :src="scope.row.goodsImg" />
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getDetail, orderReview } from "../../../api/crm/salesorder";
import { formOption } from "../../../const/crud/crm/form_option/sales_order";
import { formOption as reviewOption } from "../../../const/crud/crm/form_option/sales_order_review";
import { formOption as orderDetailOption } from "../../../const/crud/crm/form_option/sales_order_detail";

export default {
  data() {
    return {
      form: {},
      showForm: {},
      option: formOption,
      reviewOption: reviewOption,
      orderDetailOption: orderDetailOption,
      orderDetailList: [],
      btnLoading: false
    };
  },
  computed: {},
  mounted() {},
  created() {
    // let salesOrderSn = this.$route.query.res.salesOrderSn;
    // 
//     let params = {
//       salesOrderSn: salesOrderSn
//     };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => vm.getDetailReview(vm.$route.query.res.salesOrderSn));
  },
  methods: {
    getDetailReview(salesOrderSn) {
        this.form.salesOrderSn = salesOrderSn;
      getDetail({salesOrderSn:salesOrderSn})
        .then(response => {
          this.showForm = response.data.data;
          this.orderDetailList = this.showForm.detailList;
        })
        .catch(() => {
          this.goBack();
        });
    },
    handleSuccess() {
      this.btnLoading = true;
      let params = {
        salesOrderSn: this.form.salesOrderSn,
        reviewStatus: "3",
        advanceAmount: this.form.advanceAmount,
        remarks: this.form.remarks
      };
      orderReview(params).then(response => {
        if (response.data.code === 0) {
          this.$message.success("审核通过, 操作成功");
          this.goBack();
        } else {
          this.$message.success(response.data.msg);
        }
      });
    },
    handleFailed() {
      this.btnLoading = true;
      let params = {
        salesOrderSn: this.form.salesOrderSn,
        reviewStatus: "2",
        advanceAmount: this.form.advanceAmount,
        remarks: this.form.remarks
      };
      orderReview(params).then(response => {
        if (response.data.code === 0) {
          this.$message.success("审核不通过, 操作成功");
          this.goBack();
        } else {
          this.$message.success(response.data.msg);
        }
      });
    },
    goBack(time = 1000) {
      let that = this;
      setTimeout(function() {
        that.$store.commit("DEL_ONE_TAG", that.$route);
        that.$router.go(-1);
      }, time);
    }
  }
};
</script>

<style scoped lang="scss">
.crumbs {
  margin: 20px 10px;
}
</style>


